//
//  FMMainController.m
//  FreeMark
//
//  Created by gaopr on 2019/2/17.
//  Copyright © 2019年 gaopr. All rights reserved.
//

#import "FMMainController.h"
#import "FMainCell.h"
#import "UIConfig.h"
#import "FMColorPickerController.h"
#import "FMainAddView.h"

@interface FMMainController ()<UICollectionViewDataSource>
{
    UICollectionViewFlowLayout *layout;
}
@property (nonatomic, weak) UICollectionView *collectionView;
@end

@implementation FMMainController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpUI];
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    FMColorPickerController *vc = [[FMColorPickerController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
//}

- (void)setUpUI
{
    [self configAddButton];
    
    CGFloat margin = kFMargin;
    NSInteger columns = 2;
    
    CGSize viewSize = self.view.bounds.size;
    
    layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemW = (viewSize.width - (columns + 1) * margin) / columns;
    CGFloat itemH = itemW / 0.618;
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;
    layout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    
    collectionView.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    collectionView.dataSource = self;
    
    [collectionView registerClass:FMainCell.class forCellWithReuseIdentifier:@"FMainCell"];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [collectionView dequeueReusableCellWithReuseIdentifier:@"FMainCell" forIndexPath:indexPath];
}

- (void)onAddAction:(UIButton *)btn
{
    FMainAddView *addView = [[FMainAddView alloc] init];
    addView.cardSize = layout.itemSize;
    [addView showInView:self.view];
}

@end
